package com.wicket.okrframework.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wicket.okrframework.base.service.MMetadataPropertyTypeService;
import com.wicket.okrframework.base.service.dto.req.QueryObjTypeRelationDetailReq;
import com.wicket.okrframework.base.service.dto.req.QueryObjTypeRelationListReq;
import com.wicket.okrframework.common.dto.FieldInfo;
import com.wicket.okrframework.common.exception.BizException;
import com.wicket.okrframework.common.util.CommonFunctionHelper;
import com.wicket.okrframework.dal.mapper.mbg.MetadataPropertyTypeMapper;
import com.wicket.okrframework.dal.po.mbg.MetadataObjectProperty;
import com.wicket.okrframework.dal.po.mbg.MetadataPropertyType;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.ArrayList;

/**
 * @author ：
 * @since 2022/1/19 15:39
 *
 *
 * @version 1.0
 */
@Service
public class MMetadataPropertyTypeServiceImpl
        extends ServiceImpl<MetadataPropertyTypeMapper, MetadataPropertyType>
        implements MMetadataPropertyTypeService {

    @Autowired
    MetadataPropertyTypeMapper metadataPropertyTypeMapper;

    /**
   * 1-1-12查询对象属性类型关系列表[88]
   */
 @Trace(operationName = "1-1-12查询对象属性类型关系列表")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  List<MetadataPropertyType>  queryObjTypeRelationList(QueryObjTypeRelationListReq reqDto) {
     
        try {
            QueryWrapper<MetadataPropertyType> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(MetadataPropertyType.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(MetadataPropertyType.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper,likeList,fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap,likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(MetadataPropertyType.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByAsc("order_number");
queryWrapper.orderByDesc("operate_time");
            return ((List<MetadataPropertyType>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1",e,false);
        }
  }

    /**
   * 1-1-12查询对象属性类型关系详情[160]
   */
 @Trace(operationName = "1-1-12查询对象属性类型关系详情")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  MetadataPropertyType  queryObjTypeRelationDetail(QueryObjTypeRelationDetailReq reqDto) {
     
        try {
         reqDto.setIsValid("FALSE");
            QueryWrapper<MetadataPropertyType> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<MetadataPropertyType> retList = super.list(queryWrapper);
            if(retList!=null && retList.size()>0){
                MetadataPropertyType retData = retList.get(0);

                return retData;
            }
            return null;
      } catch (Exception e) {
          e.printStackTrace();
          throw new BizException("-1",e,false);
      }
  }

    /**
   * 1-1-12删除对象属性类型关系[87]
   */
 @Trace(operationName = "1-1-12删除对象属性类型关系")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  boolean  deleteObjTypeRelation(String objectPropertyTypeId ) {
     
      return super.removeById(objectPropertyTypeId);
  }

    /**
   * 1-1-12修改对象属性类型关系[86]
   */
 @Trace(operationName = "1-1-12修改对象属性类型关系")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  boolean  updateObjTypeRelation(MetadataPropertyType metadataPropertyType) {
     metadataPropertyType.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
metadataPropertyType.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
if(metadataPropertyType!=null && metadataPropertyType.getSubjectLifeCycle()!=null &&  metadataPropertyType.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            metadataPropertyType.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
          }
if(metadataPropertyType!=null && metadataPropertyType.getSubjectLifeCycle()!=null && metadataPropertyType.getSubjectLifeCycle().equals("CURRENTLY_VALID")){
             metadataPropertyType.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
            }
        return super.updateById(metadataPropertyType);
  }

    /**
   * 1-1-12新增对象属性类型关系[85]
   */
 @Trace(operationName = "1-1-12新增对象属性类型关系")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  String  addObjTypeRelation(MetadataPropertyType metadataPropertyType) {
     metadataPropertyType.setObjectPropertyTypeId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
metadataPropertyType.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
metadataPropertyType.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
metadataPropertyType.setIsArchive("FALSE");
metadataPropertyType.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
if(metadataPropertyType!=null && metadataPropertyType.getSubjectLifeCycle()!=null &&  metadataPropertyType.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            metadataPropertyType.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
          }
metadataPropertyType.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
if(metadataPropertyType!=null && metadataPropertyType.getSubjectLifeCycle()!=null && metadataPropertyType.getSubjectLifeCycle().equals("CURRENTLY_VALID")){
             metadataPropertyType.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
            }
metadataPropertyType.setIsValid("FALSE");
metadataPropertyType.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
metadataPropertyType.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
     if(super.save(metadataPropertyType)){
        return metadataPropertyType.getObjectPropertyTypeId();
     }else {
        return "-1";
     }
    
  }
}
